Public Class frmListClients

    Public selectedClients As String

    Private Sub frmListClients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        selectedClients = ""
        buildClientList()
    End Sub

    Sub buildClientList()
        'clear the tree
        treeClients.Nodes.Clear()

        'recursively build the group tree just like frmMain does
        frmMain.net.adminBuildComputerTree(treeClients, True)

        'tack on "groupings" at the end under a "Groupings" node

    End Sub

    Private Sub treeClients_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeClients.AfterCheck
        Dim i As Integer

        If e.Node.Checked Then
            If e.Node.Nodes.Count > 0 Then
                For i = 0 To e.Node.Nodes.Count - 1
                    If e.Node.Nodes(i).Nodes.Count <= 0 Then
                        e.Node.Nodes(i).Checked = True
                    End If
                Next
            End If
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        selectedClients = allCheckedBoxTags(treeClients)
        Me.Close()
    End Sub

    Function allCheckedBoxTags(ByVal tree As Windows.Forms.TreeView) As String
        Dim ids As String
        ids = findNextBranchOfCheckedNodes(tree.Nodes(0))
        Return ids
    End Function

    Function findNextBranchOfCheckedNodes(ByVal baseNode As Windows.Forms.TreeNode) As String
        Dim i As Integer
        Dim allIDs As String = ""

        For i = 0 To baseNode.Nodes.Count - 1
            If baseNode.Nodes(i).Nodes.Count = 0 And baseNode.Nodes(i).Checked Then
                If allIDs = "" Then
                    allIDs = baseNode.Nodes(i).Tag
                Else
                    allIDs = allIDs & "," & baseNode.Nodes(i).Tag
                End If
            Else
                If allIDs = "" Then
                    allIDs = findNextBranchOfCheckedNodes(baseNode.Nodes(i))
                Else
                    allIDs = allIDs & "," & findNextBranchOfCheckedNodes(baseNode.Nodes(i))
                End If
            End If
        Next

        Return allIDs
    End Function
End Class